package thitracnghiem;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Odotube
 */
public class MonThi {
    String maMonThi;
    String tenMonThi;
    static Connection conn;
    static Statement stm;
    
    public MonThi(String maMonThi, String tenMonThi){
        this.maMonThi=maMonThi;
        this.tenMonThi=tenMonThi;
    }
    
    public static void Open() throws SQLException{
        conn=DriverManager.getConnection("jdbc:odbc:ttn");
        stm=conn.createStatement();
    }
    
    public static void Close() throws SQLException{
        stm.close();
        conn.close();
    }
    
    public static ArrayList<MonThi> getDSMonThi() throws SQLException{
        ArrayList<MonThi> mList = new ArrayList<MonThi>();
        String sql = "select * from MonThi";
        ResultSet rs = stm.executeQuery(sql);
        while(rs.next()){
            mList.add(new MonThi(rs.getString(1),rs.getString(2)));
        }
        return mList;
    }
    
    public static ArrayList<MonThi> getDSMonThi(String phanQuyenMon) throws SQLException {
        if (phanQuyenMon.equals("all")) {
            return MonThi.getDSMonThi();
        } else {
            String[] strings = phanQuyenMon.split("//");
            if(strings.length==0) return null;
            ArrayList<MonThi> mList = new ArrayList<MonThi>();
            String sql = "select * from MonThi where mamonthi=";
            for(int i=0;i<strings.length;i++){
                sql+="'"+strings[i]+"'or mamonthi=";
            }
            sql=sql.substring(0,sql.length()-12);
           // sql+=")";
            System.out.println(sql);
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()) {
                mList.add(new MonThi(rs.getString(1),rs.getString(2)));
            }
            return mList;
        }
    }
    
    public static StringBuilder getViewDSMonThi(String phanQuyenMon) throws SQLException{
        StringBuilder ms = new StringBuilder();
        ArrayList<MonThi> mList = MonThi.getDSMonThi(phanQuyenMon);
        ms.append("<p align=center><table border=2><th>Ma mon thi<th>Ten mon thi<th>Xoa");
        for(int i=0;i<mList.size();i++){
            ms.append("<tr><td>"+mList.get(i).getMaMonThi()+"</td><td>"+mList.get(i).getTenMonThi()+"</td><td><a href=/ThiTracNghiem/XuLy?yc=xoamonthi&mamonthi="+mList.get(i).getMaMonThi() +">Xoa</td></tr>");
        }
        ms.append("</table></p>");
        return ms;
    }
    
    public static int insMonThi(String maMonThi, String tenMonThi) throws SQLException{
        String sql="insert into MonThi values('"+maMonThi+"',N'"+tenMonThi+"')";
        return stm.executeUpdate(sql);
    }
    
    public static int delMonThi(String maMonThi) throws SQLException{
        String sql="delete MonThi where mamonthi='"+maMonThi+"'";
        return stm.executeUpdate(sql);
    }
    public String getMaMonThi(){
        return this.maMonThi;
    }
    
    public void setMaMonThi(String maMonThi){
        this.maMonThi=maMonThi;
    }
    
    public String getTenMonThi(){
        return this.tenMonThi;
    }
    
    public void setTenMonThi(String tenMonThi){
        this.tenMonThi=tenMonThi;
    }
    
    public static void main(String args[]){
        try {
            MonThi.Open();
             System.out.println(MonThi.getDSMonThi("cplus//java").size());
            MonThi.Close();
           
        } catch (SQLException ex) {
            Logger.getLogger(MonThi.class.getName()).log(Level.SEVERE, null, ex);
        }
        
                
    }
    
}
